package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ICompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.sql.ResultSet;
import java.sql.SQLException;

public class CompanyDaoImpl implements ICompanyDao {
    @Override
    public CompanyModel getCompany() {
        String sql = "select * from company";
        ResultSet rs =  JDBCUtils.getResultSet(sql);
        CompanyModel company = null;
        try {
            if (rs.next()){
                company = new CompanyModel();
                company.setId(rs.getInt("id"));
                company.setAd(rs.getString("ad"));
                company.setAddress(rs.getString("address"));
                company.setCopyright(rs.getString("copyright"));
                company.setCpy_intro(rs.getString("cpy_intro"));
                company.setEmail(rs.getString("email"));
                company.setEnglish_name(rs.getString("english_name"));
                company.setFax(rs.getString("fax"));
                company.setIntro(rs.getString("intro"));
                company.setLogo(rs.getString("logo"));
                company.setName(rs.getString("name"));
                company.setPhone(rs.getString("phone"));
                company.setWebsite(rs.getString("website"));
                company.setWork_time(rs.getString("work_time"));
            }
            rs.close();
            System.out.println("mysql");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return company;
    }

    @Override
    public int editCompany(CompanyModel company) {
        String sql = "update company set ad='"+company.getAd()
                +"',address='"+company.getAddress()
                +"',copyright='"+company.getCopyright()
                +"',cpy_intro='"+company.getCpy_intro()
                +"',email='"+company.getEmail()
                +"',english_name='"+company.getEnglish_name()
                +"',fax='"+company.getFax()
                +"',intro='"+company.getIntro()
                +"',logo='"+company.getLogo()
                +"',name='"+company.getName()
                +"',phone='"+company.getPhone()
                +"',website='"+company.getWebsite()
                +"',work_time='"+company.getWork_time()
                +"',update_time='"+company.getUpdateTime()+"'";
        Jedis jedis = new Jedis();
        jedis.del("company_id");
        return JDBCUtils.excute(sql);
    }

    @Override
    public CompanyModel getCompanyByRedis() {
        Jedis jedis = new Jedis();
        CompanyModel company = new CompanyModel();
        company.setId(Integer.valueOf(jedis.get("company_id")));
        company.setAd(jedis.get("ad"));
        company.setAddress(jedis.get("company_address"));
        company.setCopyright(jedis.get("copyright"));
        company.setCpy_intro(jedis.get("cpy_intro"));
        company.setEmail(jedis.get("company_email"));
        company.setEnglish_name(jedis.get("english_name"));
        company.setFax(jedis.get("fax"));
        company.setIntro(jedis.get("company_intro"));
        company.setLogo(jedis.get("logo"));
        company.setName(jedis.get("company_name"));
        company.setPhone(jedis.get("company_phone"));
        company.setWebsite(jedis.get("website"));
        company.setWork_time(jedis.get("work_time"));
        System.out.println("redis");
        return company;
    }

    @Override
    public void setCompanyInRedis(CompanyModel company) {
        Jedis jedis = new Jedis();
        jedis.set("company_id", String.valueOf(company.getId()));
        jedis.set("ad",company.getAd());
        jedis.set("company_address",company.getAddress());
        jedis.set("copyright",company.getCopyright());
        jedis.set("company_email",company.getEmail());
        jedis.set("english_name",company.getEnglish_name());
        jedis.set("fax",company.getFax());
        jedis.set("company_intro",company.getIntro());
        jedis.set("cpy_intro",company.getCpy_intro());
        jedis.set("logo",company.getLogo());
        jedis.set("company_name",company.getName());
        jedis.set("company_phone",company.getPhone());
        jedis.set("website",company.getWebsite());
        jedis.set("work_time",company.getWork_time());
    }
}
